using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Windows.Media.Imaging;
using System.Xml.Linq;
namespace INTEX.DataBase.CE
{
    public partial class Mapper
    {
    /*
      var qZUTEI = from m in mtemp.ZUTEI select m;
       DBMapper.MapZUTEI(qZUTEI.ToList(), true);
       */
        public List<ZUTEI> MapZUTEI(XElement item2, bool mergedb)
        {
            List<ZUTEI> result = new List<ZUTEI>();
            var item4 = from item3 in item2.Elements()
                        select new ZUTEI
                        {  
                            MAINKEY = (string)item3.Element("MAINKEY"),
                            ZUART = (string)item3.Element("ZUART"),
                            PHOTO = (string)item3.Element("PHOTO")
                        }; 
                        
          result = item4.ToList();
          MapZUTEI(result, mergedb);
          return result;
        }
        ///DBMapper.MapZUTEI(mtemp.ZUTEI.ToList<ZUTEI>(), true);        
       public void MapZUTEI(List<ZUTEI> result, bool mergedb)
        {
          if (mergedb)
            {     
            
                int loaded = 0;
                int page = 500;
                int count = result.Count();
                while (true)
                {
                    if (count - loaded < page) 
                    {
                        page = count - loaded;
                    }
                    var query =
                        (from ZUTEI row in result.AsQueryable()
                         select row).Skip(loaded).Take(page);
               var rowsexist = from u in LocalDB.ZUTEI.ToList().AsQueryable()
                                           join uc in query.AsQueryable() on  u.MAINKEY equals  uc.MAINKEY
                                           select u;
                LocalDB.ZUTEI.DeleteAllOnSubmit(rowsexist);
                LocalDB.SubmitChanges();
                LocalDB.ZUTEI.InsertAllOnSubmit(query);
                LocalDB.SubmitChanges();
                 loaded += page;
                if (count == loaded)
                        break;
                }
            }
       }
    }
    [Table(Name = "ZUTEI")]
    public class ZUTEI : TBBASE
    {
        private string _MAINKEY = "0";
        [Column(IsPrimaryKey = true, CanBeNull = false)]
        public string MAINKEY
        {
            get
            {
                return _MAINKEY;
            }
            set
            {
                if (value != _MAINKEY)
                {
                    _MAINKEY = value;
                    NotifyPropertyChanging("MAINKEY");                 
                }
            }
        }
        private string _Photo = "YES";
        [Column]
        public string PHOTO
        {
            get
            {
                return _Photo;
            }
            set
            {
                if (value != _Photo)
                {
                    _Photo = value;
                    NotifyPropertyChanging("PHOTO");
                }
            }
        } 
        [Column(IsVersion = true)]
        private Binary _version;
   
        private string  _ZUART="0";
        [Column]
        public string ZUART
        {
            get
            {
                return _ZUART;
            }
            set
            {
                if (value != _ZUART)
                {
                     _ZUART = value;
                    NotifyPropertyChanged("ZUART");
                }
            }
        }
      public void ConvertToFile(IsolatedStorageFile ISO,ObservableCollection<ZUTEI> ListInput, string filep = "", string setguid="")
        {           
            try
            {
                string localfile = filep;
                if (filep.Trim() == "")
                {
                    if (setguid.Trim() == "")
                        setguid = Guid.NewGuid().ToString();
                    localfile = "Data\\PhoneDB\\ZUTEI\\ZUTEI" +setguid + ".xml";
                }
                string folder = "Data\\PhoneDB\\ZUTEI" ;
                if (!ISO.DirectoryExists(folder))
                {
                    ISO.CreateDirectory(folder);
                }
                ISO.DeleteFile(localfile);
                StreamWriter sw = new StreamWriter(ISO.CreateFile(localfile));
                XDocument xdoc = new XDocument();
                //Root Element
                XElement root = new XElement("ZUTEI");
                if (setguid.Trim() != "")
                {
                    root = new XElement("ITX"+setguid);
                }
                //Add root to file
                xdoc.Add(root);
                foreach (ZUTEI row in ListInput)
                {
                    XElement f = new XElement("ZUTEI");
      